<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="4">
        <el-select v-model="conditions.province" placeholder="请选择省" size="small" @change="cityFn">
          <el-option v-for="item in provinceOptions" :key="item.id" :label="item.name" :value="item.id"> </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="conditions.city" placeholder="请选择市" size="small" @change="areaFn">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id"> </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="conditions.area" placeholder="请选择区" size="small">
          <el-option v-for="item in areaOptions" :key="item.id" :label="item.name" :value="item.id"> </el-option>
        </el-select>
      </el-col>
      <el-col :span="3"><el-input v-model="conditions.school_title" placeholder="请输入学校名称关键字" size="small"></el-input></el-col>
      <el-col :span="3"><el-input v-model="conditions.plan_title" placeholder="请输入计划名称" size="small"></el-input></el-col>
      <el-col :span="3"><el-input v-model="conditions.title" placeholder="请输入量表或组合名称" size="small"></el-input></el-col>
      <el-col :span="2"><el-button type="primary" size="small" @click="searchFn()">搜索</el-button></el-col>
    </el-row>
    <el-row v-if="this.menuList.includes('storeShoolEval')" class="btnBox">
      <el-col :span="2" :offset="22"><el-button type="primary" size="small" @click="addPlan">新增计划</el-button></el-col>
    </el-row>
    <div class="table">
      <el-table :data="tableData" :menuList="menuList" v-loading="tableConfig.load" :style="tableConfig.style" :max-height="tableConfig.maxHeight" :border=tableConfig.border :size="tableConfig.size">
        <el-table-column v-if="tableConfig.selection === true" type="selection" width="55"> </el-table-column>
          <template v-for="(item, index) of tableConfig.config">
            <el-table-column v-if="item.id === 'operation'" :key="index" :prop="item.id" :label="item.name" :align="tableConfig.align?tableConfig.align:'center'" :width="item.width">
              <template slot-scope="scope">
                <el-button @click="examine('edit', scope.row)" v-if="scope.row.status_title !== '已结束' && menuList.includes('editSchoolEval')" type="text" size="small">编辑</el-button>
                <el-button @click="examine('progress', scope.row)" v-if="menuList.includes('progressSchool')" type="text" size="small">测评进度</el-button>
                <el-button @click="examine('copy', scope.row)" v-if="menuList.includes('copySchoolEval')" type="text" size="small">复制</el-button>
                <el-button @click="examine('del', scope.row)" v-if="scope.row.status_title === '进行中' && menuList.includes('terminationSchoolEva')" type="text" size="small">终止</el-button>
                <el-button @click="create_FN(scope.row)" v-if="scope.row.plan_type === 1" :disabled="scope.row.report_status === 2" type="text" size="small">生成报告</el-button>
              </template>
            </el-table-column>
            <el-table-column v-else :label="item.name" :key="index" :prop="item.id" :align="tableConfig.align?tableConfig.align:'center'" :width="item.width" :type="item.type"></el-table-column>
          </template>
        </el-table>
    </div>
    <div class="page">
      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="this.limit" layout="total, prev, pager, next" :total="total"> </el-pagination>
    </div>
    <add-plan :isAddPlan="this.isAddPlan" @noAddPlan="noAddPlan"></add-plan>
  </div>
</template>
<script>
import {APISCHOOLWHOLEREGION, APIPLANGETPLANLIST, APISCHOOLPLANTERMINATIONPLAN, getMakeSchoolReport} from '@/common/api.js'
import addPlan from './components/addPlan.vue'
export default {
  components: {addPlan},
  data () {
    return {
      menuList: [],
      isAddPlan: false,
      isEditPlan: false,
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
      conditions: {
        province: '',
        city: '',
        area: '',
        title: '',
        school_title: '',
        plan_title: ''
      },
      tableConfig: {
        load: false,
        size: 'small', // 表格大小
        style: 'width:100%', // 表格宽度
        align: 'center',
        selection: false,
        border: true,
        config: [
          {id: 'id', name: 'ID'},
          {id: 'school_title', name: '学校名称'},
          {id: 'province_title', name: '省份'},
          {id: 'city_title', name: '城市'},
          {id: 'area_title', name: '区/县'},
          {id: 'title', name: '计划名称'},
          {id: 'plan_title', name: '组合或量表名称'},
          {id: 'evaluated_number', name: '测评人次'},
          {id: 'completion_schedule', name: '完成进度'},
          {id: 'gauge_number', name: '测评量表数量'},
          {id: 'question_number', name: '题量'},
          {id: 'completion_schedule', name: '用时'},
          {id: 'status_title', name: '状态'},
          {id: 'start_time', name: '开始时间'},
          {id: 'end_time', name: '结束时间'},
          {id: 'operation', name: '操作'}
        ]
      },
      tableData: [],
      id: '',
      currentPage: 1,
      total: 0,
      page: 1,
      limit: 10
    }
  },
  created () {
    this.menuList = sessionStorage.getItem('list')
    this.getWholeRegion()
    this.tableList()
  },
  methods: {
    getWholeRegion () {
      APISCHOOLWHOLEREGION().then((res) => {
        this.provinceOptions = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    cityFn () {
      this.conditions.city = ''
      this.conditions.area = ''
      APISCHOOLWHOLEREGION({
        parent_id: this.conditions.province
      }).then((res) => {
        this.cityOptions = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    areaFn () {
      this.conditions.area = ''
      APISCHOOLWHOLEREGION({
        parent_id: this.conditions.city
      }).then((res) => {
        this.areaOptions = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    tableList () {
      APIPLANGETPLANLIST({
        province: this.conditions.province,
        city: this.conditions.city,
        area: this.conditions.area,
        title: this.conditions.title,
        school_title: this.conditions.school_title,
        plan_title: this.conditions.plan_title,
        page: this.page
      }).then((res) => {
        if (res.data !== null) {
          this.tableData = res.data.list
          this.total = res.data.totalNumber
          this.currentPage = res.data.page
        } else {
          this.tableData = []
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    searchFn () {
      this.tableList()
    },
    examine (id, val) {
      if (id === 'distribution') {
        this.id = val.id
      } else if (id === 'edit') {
        this.$router.push({path: '/editSchoolPlan', query: {id: val.id, schoolId: val.school_id}})
      } else if (id === 'copy') {
        this.$router.push({path: '/editCopySchoolPlan', query: {id: val.id, schoolId: val.school_id}})
      } else if (id === 'del') {
        this.delfn(val)
      } else if (id === 'progress') {
        this.$router.push({path: '/schoolPlanProgress', query: {id: val.id, schoolId: val.school_id}})
      }
    },
    handleCurrentChange (val) { // 分页
      this.page = val
      this.tableList()
    },
    addPlan () {
      this.isAddPlan = true
    },
    noAddPlan (list) {
      this.isAddPlan = false
      if (list.type === 1 && list.school !== '') {
        this.$router.push({path: '/addSchoolPlan', query: {id: list.school, title: list.title}})
      }
    },
    delfn (val) {
      APISCHOOLPLANTERMINATIONPLAN({
        plan_id: val.id,
        school_id: val.school_id
      }).then((res) => {
        this.$message.success(res.msg)
        this.tableList()
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    // 生成报告
    create_FN (val) {
      console.log(val)
      getMakeSchoolReport({plan_id: val.id}).then(res => {
        if (res.data.status === 1) {
          this.$message.success(res.data.msg)
          this.tableList()
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    }
  }
}
</script>
